package com.alipay.sofa.award.endpoint.impl;

import com.alibaba.common.lang.StringUtil;
import com.alipay.sofa.award.endpoint.exception.CommonException;
import com.alipay.sofa.award.endpoint.facade.AwardRecordsRestFacade;
import com.alipay.sofa.award.endpoint.response.RestSampleFacadeResp;
import com.alipay.sofa.award.record.common.dal.dataobject.AwardRecord;
import com.alipay.sofa.award.record.facade.AwardRecordFacade;
import com.alipay.sofa.award.record.facade.PagedQueryResult;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * AwardRecordsRestImpl
 *
 * @author yangguanchao
 * @since 2018/09/03
 */
public class AwardRecordsRestImpl implements AwardRecordsRestFacade {

    @Autowired
    private AwardRecordFacade awardRecordFacade;

    @Override
    public RestSampleFacadeResp<PagedQueryResult<AwardRecord>> getPagedAwardRecords(int currentPage,
                                                                                    int pageSize)
                                                                                                 throws CommonException {
        PagedQueryResult<AwardRecord> pagedQueryResult = this.awardRecordFacade.getAllPagedRecords(
            currentPage, pageSize);
        //insensitive handled
        List<AwardRecord> awardRecordList = pagedQueryResult.getList();
        pagedQueryResult.setList(this.insensitiveAwardRecordList(awardRecordList));
        return new RestSampleFacadeResp<PagedQueryResult<AwardRecord>>(pagedQueryResult);
    }

    @Override
    public RestSampleFacadeResp<PagedQueryResult<AwardRecord>> getSelfPagedAwardRecords(String userId,
                                                                                        int currentPage,
                                                                                        int pageSize)
                                                                                                     throws CommonException {
        PagedQueryResult<AwardRecord> pagedQueryResult = this.awardRecordFacade
            .getSelfPagedRecords(userId, currentPage, pageSize);
        List<AwardRecord> awardRecordList = pagedQueryResult.getList();
        pagedQueryResult.setList(this.insensitiveAwardRecordList(awardRecordList));

        return new RestSampleFacadeResp<PagedQueryResult<AwardRecord>>(pagedQueryResult);
    }

    private List<AwardRecord> insensitiveAwardRecordList(List<AwardRecord> awardRecordList) {
        if (awardRecordList == null || awardRecordList.size() <= 0) {
            return awardRecordList;
        }
        for (AwardRecord awardRecord : awardRecordList) {
            String nickName = awardRecord.getNickName();
            //insensitive
            if (StringUtil.isNotBlank(nickName)) {
                int middleIndex = nickName.length() / 2;
                String newNickName = nickName.substring(0, middleIndex) + "*";
                if (middleIndex + 1 <= nickName.length()) {
                    newNickName = newNickName
                                  + nickName.substring(middleIndex + 1, nickName.length());
                }
                awardRecord.setNickName(newNickName);
            }
        }
        return awardRecordList;
    }
}
